<template>
  <el-dialog :title="this.type==='add'?'新增用户信息':'修改用户信息'" :visible.sync="activeTopTemp" :close-on-click-modal="false" @close="dialogFormVisible=false">
    <el-form :model="formObj" ref="formObj" :rules="rules">
      <el-form-item label="用户ID">
        <el-input v-model="formObj.id" :disabled="true" placeholder="新增用户时系统分配ID,不可指定"></el-input>
      </el-form-item>
      <el-form-item label="用户账号" prop="account">
        <el-input v-model="formObj.account" autocomplete="off" :disabled="this.type==='update'"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="formObj.password" autocomplete="off" :disabled="this.type==='update'"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="formObj.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="formObj.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="formObj.userType" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doAddUser, doUpdateUser} from "@/api/user";

export default {
  name: "userDialog",
  props:{
    type:String,
    dialogFormVisible:Boolean,
    user:Object
  },
  data() {
    return{
      activeTopTemp:this.dialogFormVisible,
      formObj:this.user,
      rules: {
        account:[ { required: true, message: '请输入账号', trigger: 'blur' }],
        password:[ { required: true, message: '请输入密码', trigger: 'blur' }],
        userName:[ { required: true, message: '请输入用户名', trigger: 'blur' }],
        email:[ { required: true, message: '请输入邮箱', trigger: 'blur' }],
        userType:[ { required: true, message: '请输入用户类型', trigger: 'blur' }]
      },
      options: [{
        value: 1,
        label: '普通用户'
      }, {
        value: 2,
        label: '管理员'
      }]
    }
  },
  methods:{
      onSubmit() {
        //参数校验
        this.$refs.formObj.validate((valid) => {
          if (valid) {
            if (this.type==='add'){
                doAddUser(this.formObj).then(res=>{
                    if (res.code===200){
                      this.activeTopTemp=false;
                      this.$emit('change',true)
                    }
                })
            }else{
              doUpdateUser(this.formObj).then(res=>{
                  if (res.code===200){
                    this.activeTopTemp=false;
                    this.$emit('change',true)
                  }
              })
            }
          } else {
            return false;
          }
        })
      }
  },watch:{
    dialogFormVisible(){
      this.activeTopTemp=this.dialogFormVisible;
    },
    user(){
      this.formObj.userType=this.user.userType;
      this.formObj = this.user;
    }
  }
}
</script>

<style scoped>

</style>